<?php
/**
 * Created by PhpStorm.
 * User: caicm
 * Date: 2017/6/14
 * Time: 17:22
 */

namespace WMApi\Model;


use Think\Model;

class WmSellerModel extends Model {

    /**
     * 写入商家表
     * Created By caicm
     * @param int $user_id
     * @param string $seller_token
     * @return mixed
     */
    public function add_seller($user_id=0, $seller_token=''){

        $data = array(
            'user_id' => $user_id,
            'seller_token' => $seller_token,
            'withdraw_password' => '',
            'source' => get_source(),
            'version' => get_version()
        );

        return $this->add($data);
    }

    /**
     * 获取商家信息
     * Created By caicm
     * @param int $seller_id
     * @param int $user_id
     * @return mixed
     */
    public function find_seller_info($seller_id=0, $user_id=0){

        $where = array(
            'seller_id' => $seller_id,
            'user_id' => $user_id
        );
        if($seller_id==0) unset($where['seller_id']);
        if($user_id==0) unset($where['user_id']);

        return $this->where($where)->find();

    }

    /**
     * 获取商家user_id
     * Created By caicm
     * @param int $seller_id
     * @return bool
     */
    public function get_seller_user_id($seller_id=0){
        $where = array(
            'seller_id' => $seller_id
        );
        return $this->where($where)->getField('user_id');
    }

    /**
     * 修改商家token
     * Created By caicm
     * @param int $seller_id
     * @param string $seller_token
     * @return bool
     */
    public function update_seller_token($seller_id=0, $seller_token=''){

        $where = array(
            'seller_id' => $seller_id
        );

        $data = array(
            'seller_token' => $seller_token
        );

        return $this->where($where)->data($data)->save();
    }

    /**
     * 修改商家提现密码
     * Created By caicm
     * @param int $seller_id
     * @param string $withdraw_password
     * @return bool
     */
    public function update_withdraw_password($seller_id=0, $withdraw_password=''){

        $where = array(
            'seller_id' => $seller_id
        );

        $data = array(
            'withdraw_password' => $withdraw_password
        );

        return $this->where($where)->data($data)->save();

    }

    /**
     * 获取商家账户余额
     * Created By caicm
     * @param int $seller_id
     * @return bool
     */
    public function get_account_money($seller_id=0){
        $where = array(
            'seller_id' => $seller_id
        );
        return $this->where($where)->getField('account_money');
    }

    /**
     * 减去商家账户余额
     * Created By caicm
     * @param int $seller_id
     * @param int $money
     * @return bool
     */
    public function dec_account_money($seller_id=0, $money=0){
        $where = array(
            'seller_id' => $seller_id
        );
        return $this->where($where)->setDec('account_money', $money);
    }

    /**
     * 增加商家账户余额
     * Created By caicm
     * @param int $seller_id
     * @param int $money
     * @return bool
     */
    public function add_account_money($seller_id=0, $money=0){
        $where = array(
            'seller_id' => $seller_id
        );
        return $this->where($where)->setInc('account_money', $money);
    }

    /**
     * 减去商家冻结余额
     * Created By caicm
     * @param int $seller_id
     * @param int $money
     * @return bool
     */
    public function dec_freeze_money($seller_id=0, $money=0){
        $where = array(
            'seller_id' => $seller_id
        );
        return $this->where($where)->setDec('freeze_money', $money);
    }

    /**
     * 增加商家冻结余额
     * Created By caicm
     * @param int $seller_id
     * @param int $money
     * @return bool
     */
    public function add_freeze_money($seller_id=0, $money=0){
        $where = array(
            'seller_id' => $seller_id
        );
        return $this->where($where)->setInc('freeze_money', $money);
    }

    /**
     * 减去商家未结算余额
     * Created By caicm
     * @param int $seller_id
     * @param int $money
     * @return bool
     */
    public function dec_unavailable_money($seller_id=0, $money=0){
        $where = array(
            'seller_id' => $seller_id
        );
        return $this->where($where)->setDec('unavailable_money', $money);
    }

    /**
     * 增加商家未结算余额
     * Created By caicm
     * @param int $seller_id
     * @param int $money
     * @return bool
     */
    public function add_unavailable_money($seller_id=0, $money=0){
        $where = array(
            'seller_id' => $seller_id
        );
        return $this->where($where)->setInc('unavailable_money', $money);
    }

}